import {toast } from 'nc-lightapp-front';

/**
 * 表格扩展按钮click事件
 */
import {headSumHandle} from "./commonEvent";
import {setDefautValueAfterBodyAdd} from '../initTemplate'

function tableExtendButtonClick() {
    let returnObj = {};
    for(let name in window.presetVar.body){
        let areaId = window.presetVar.body[name];
        if(areaId==='jk_busitem'){
            returnObj[areaId +'_Delete']={
                beforeClick:(record, index) => {
                    console.log('before delete record:');
                    if((record.status == '1' || record.status == '0') && record.values.pk_mtapp_detail&& record.values.pk_mtapp_detail.value) {
                        toast({content:'由申请生成的借款明细不允许删除,请知悉!', color:'danger'})
                        return false
                    }
                    return true
                },
                afterClick: (record) => {
                    if((record.status == 1 || record.status == 0) && record.values.pk_mtapp_detail&& record.values.pk_mtapp_detail.value) {
                    } else {
                        headSumHandle.call(this);
                    }
                }
            };
            returnObj[areaId +'_Copy']={
                afterClick: () => {
                    headSumHandle.call(this);
                }
            };
            returnObj[areaId + '_Insert'] = {
                afterClick: (record, index) => {
                    clearPropsAboutInvoices.call(this, areaId, index+1)
                    setDefautValueAfterBodyAdd.call(this, this.props, areaId, index);
                }
            };

        }
    }
    return returnObj;
}

//清空发票相关的字段,处理行数据,模板没有的字段,使用setValByKeyAndIndex是无法清空的
export function clearPropsAboutInvoices(tableCode, index) {
    const filterKeys = [ "pk_erminvoice", "pk_erminvoice_b", "fphm", "fpdm", "fplxpk"]
    const tableData = this.props.cardTable.getVisibleRows(tableCode)
    if(tableData[index] && tableData[index].values) {
        let obj = {...tableData[index].values }
        filterKeys.forEach(key => {
            obj[key] = {display: null, value: null}
        })
        tableData[index].values = obj
    }
    this.props.cardTable.setTableDataWithResetInitValue(tableCode, {rows: tableData})
}

export default tableExtendButtonClick;